//
//  MYGMinImageCell.swift
//  MYDoctor
//
//  Created by 小火科技 on 2024/4/10.
//

import UIKit

class MYGMinImageCell: UITableViewCell {
    var delMico:(()->Void)?
    var model: MYGMicrosrationDyModel? {
        didSet {
            
            guard let item = model else  {
                return
            }
            
            let time = item.updated_at
            var M :Int = 1
            var D :Int = 1
            if let date = time.date(withFormat: "yyyy-MM-dd HH:mm:ss") {
                let calendar = Calendar.current
                let components = calendar.dateComponents([.month], from: date)
                let componentsDay = calendar.dateComponents([.day], from: date)
                if let month = components.month {
                    //print("月份是：\(month)")
                    M = month
                }
                if let day = componentsDay.day {
                    //print("月份是：\(day)")
                    D = day
                }
            } else {
                print("日期字符串格式不正确")
            }
            timeLabel.rz.colorfulConfer { confer in
                confer.text("\(D)")?.font(.medium(18)).textColor(.rgba(54, 53, 56, 1))
                confer.text("\(M)月")?.font(.regular(10)).textColor(.rgba(54, 53, 56, 1))
            }
            
            
            if item.medias.count > 0 {
                let name = item.medias.first!
                if item.type == 2 {
                    playBtn.isHidden = false
                    bkImageView.videoImageWithvideoURL(URL(string:name)!, atTime: 1000) { _ in
//                        guard let image = image, let self = self else {
//                            return
//                        }
                        //self.bkImageView.image = image
                    }
                    
                }else {
                    playBtn.isHidden = true
                    bkImageView.sd_setImage(with: URL(string: "\(name)"), placeholderImage: UIImage(named: "Chat/Function/image"))
                }
                
            }else {
                playBtn.isHidden = true
                bkImageView.image = UIImage(named: "Chat/Function/image")
            }
            
            textView.text = item.content
            
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initUI(){
        contentView.backgroundColor = .rgba(255, 255, 255, 1)
        
        contentView.addSubview(timeLabel)
        contentView.addSubview(bkImageView)
        contentView.addSubview(playBtn)
        contentView.addSubview(perButton)
        contentView.addSubview(textView)
        contentView.addSubview(delButton)
        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(22.5)
            make.top.equalTo(14.5)
            make.height.equalTo(25)
        }
        bkImageView.snp.makeConstraints { make in
            make.left.equalTo(75.5)
            make.top.equalTo(17.5)
            make.width.height.equalTo(90)
        }
        
        playBtn.snp.makeConstraints { make in
            make.edges.equalTo(bkImageView)
        }
        
        perButton.snp.makeConstraints { make in
            make.width.height.equalTo(14)
            make.bottom.equalTo(bkImageView.snp.bottom).offset(-4)
            make.right.equalTo(bkImageView.snp.right).offset(-6)
        }
        
        textView.snp.makeConstraints { make in
            make.left.equalTo(bkImageView.snp.right).offset(6.5)
            make.top.equalTo(bkImageView)
            make.right.equalToSuperview().offset(-35)
            make.height.equalToSuperview().offset(-20)
        }
        
        delButton.snp.makeConstraints { make in
            make.bottom.equalTo(bkImageView)
            make.right.equalToSuperview().offset(-35)
            make.width.equalTo(30)
        }
        
        perButton.isHidden = true
    }

    private lazy var timeLabel: UILabel = {
        let label = UILabel()
        label.rz.colorfulConfer { confer in
            confer.text("22")?.font(.medium(18)).textColor(.rgba(54, 53, 56, 1))
            confer.text("8月")?.font(.regular(10)).textColor(.rgba(54, 53, 56, 1))
        }
        return label
    }()
    
    private lazy var bkImageView:UIImageView = {
        let bkView = UIImageView()
//        bkView.backgroundColor = .red
        bkView.layer.cornerRadius = 4
        bkView.layer.masksToBounds = true
        return bkView
    }()
    
    private lazy var perButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "per"), for: .normal)
        return btn
    }()
    
    private lazy var textView:UITextView = {
        let tw = UITextView()
        tw.isEditable = false
        tw.text = "文案文案文案文案文案文案文案文案文案文案文案文案文案文案文案文案文案文案文案文案文案文案文案文案"
        tw.textColor = .rgba(44, 44, 44, 1)
        tw.font = .regular(14)
        tw.isUserInteractionEnabled = false
        return tw
    }()
    private lazy var delButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("删除", for: .normal)
        btn.setTitleColor(.rgba(57, 109, 250, 1), for: .normal)
        btn.titleLabel?.font = .regular(11)
        btn.addTarget(self, action: #selector(delBtn), for: .touchUpInside)
        return btn
    }()
    
    
    private var playBtn:UIButton = {
        let playBtn = UIButton()
        playBtn.setImage(UIImage(named: "MYG_home_play"), for: .normal)
        playBtn.isUserInteractionEnabled = false
        return playBtn
    }()
    
    @objc private func delBtn()  {
        MYGLoading.show()
        MMYGNetwork<MYGMicrosrationApi,MYGEmptyModel>.request(target: .delPersonMico(id: model!.id)) { _ in
            MYGLoading.hide()
            MMYGHUD.showToast(text: "删除成功")
            
            NotificationCenter.default.post(name: NSNotification.Name.init("reloadDyTable"), object: nil)
            
        } failed: { _, error in
            MYGLoading.hide()
            MMYGHUD.showToast(text: error)
        }
    }
    
}
